package cn.gemiman.daoyi.module.iot.controller.admin.equipmentmsg;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.iot.controller.admin.equipmentmsg.vo.*;
import cn.gemiman.daoyi.module.iot.dal.dataobject.equipmentmsg.EquipmentMsgDO;
import cn.gemiman.daoyi.module.iot.service.equipmentmsg.EquipmentMsgService;

@Tag(name = "管理后台 - 设备消息")
@RestController
@RequestMapping("/iot/equipment-msg")
@Validated
public class EquipmentMsgController {

    @Resource
    private EquipmentMsgService equipmentMsgService;

    @PostMapping("/create")
    @Operation(summary = "创建设备消息")
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:create')")
    public CommonResult<Long> createEquipmentMsg(@Valid @RequestBody EquipmentMsgSaveReqVO createReqVO) {
        return success(equipmentMsgService.createEquipmentMsg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备消息")
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:update')")
    public CommonResult<Boolean> updateEquipmentMsg(@Valid @RequestBody EquipmentMsgSaveReqVO updateReqVO) {
        equipmentMsgService.updateEquipmentMsg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:delete')")
    public CommonResult<Boolean> deleteEquipmentMsg(@RequestParam("id") Long id) {
        equipmentMsgService.deleteEquipmentMsg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:query')")
    public CommonResult<EquipmentMsgRespVO> getEquipmentMsg(@RequestParam("id") Long id) {
        EquipmentMsgDO equipmentMsg = equipmentMsgService.getEquipmentMsg(id);
        return success(BeanUtils.toBean(equipmentMsg, EquipmentMsgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备消息分页")
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:query')")
    public CommonResult<PageResult<EquipmentMsgRespVO>> getEquipmentMsgPage(@Valid EquipmentMsgPageReqVO pageReqVO) {
        PageResult<EquipmentMsgDO> pageResult = equipmentMsgService.getEquipmentMsgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentMsgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备消息 Excel")
    @PreAuthorize("@ss.hasPermission('iot:equipment-msg:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentMsgExcel(@Valid EquipmentMsgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentMsgDO> list = equipmentMsgService.getEquipmentMsgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备消息.xls", "数据", EquipmentMsgRespVO.class,
                        BeanUtils.toBean(list, EquipmentMsgRespVO.class));
    }

}